import danmaku from './danmaku';
import utils from '../ass/utils';
import logger from '../core/logger';
import { IDataSource } from '@ohos/danmakuflamemaster';

export class danmakuSource implements IDataSource<[]> {
  private mArray: danmaku[] = [];

  constructor(reply: any) {
    try {
      if (reply != null && reply.elems.length > 0) {
        reply.elems.forEach((elem) => {
          var item = new danmaku();
          item.index = elem.id;
          item.danmakuType = elem.mode;
          item.textSize = elem.fontsize;
          item.textColor = elem.color;
          item.textShadowColor = utils.getShadowColor(elem.color);
          item.text = elem.content;
          item.time = elem.progress;
          if (elem.mode == 7) {
            item.alphaDuration = 4500;
            item.beginAlpha = 249;
            item.beginX = 1;
            item.beginY = 500;
            item.duration = 4500;
            item.endAlpha = 25;
            item.endX = 40;
            item.endY = 268;
            item.rotationY = 360;
            item.rotationZ = 360;
            item.translationDuration = 500;
          }
          this.mArray.push(item);
        });
      }
    } catch (e) {
      logger.error(e);
    }
  }

  public release() {
    this.mArray = null;
  }

  public data() {
    return this.mArray
  }
}